package com.gujiangbo.es;

import com.gujiangbo.es.entity.School;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.search.SearchHit;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author: gujiangbo.co
 * @Contact: 646832012@qq.com
 * @Date: 2022/4/28 10:29
 * @Version: 1.0
 * @Description: 使用es 操作文档
 */
public class JavaEsShoppingOps {

    /**
     * 创建索引
     */
    @Test
    public void testCreateIndices() {
        CreateIndexResponse result = ElasticsearchUtils.createIndices("school");
        System.out.println(result.isAcknowledged());
        // 关闭资源
        ElasticSearchClient.close();

    }

    /**
     * 获取索引对象信息
     */
    @Test
    public void testGetINdicesObj() {
        // 获取索引对象信息
        GetIndexResponse indices = ElasticsearchUtils.getIndicesObj("school");
        System.out.println("Indices[]:" + Arrays.toString(indices.getIndices()));
        System.out.println("aliases:" + indices.getAliases());
        System.out.println("dataStreams:" + indices.getDataStreams());
        Map<String, MappingMetadata> map = indices.getMappings();
        for (Map.Entry e : map.entrySet()) {
            System.out.println(e.getKey() + "----" + e.getValue());
        }
        System.out.println(indices.getDefaultSettings());
        System.out.println(indices.getSettings());
        // 关闭资源
        ElasticSearchClient.close();
    }

    /**
     * 删除索引信息
     */
    @Test
    public void testDelIndices() {
        // 删除索引信息
        AcknowledgedResponse response = ElasticsearchUtils.deleteIndices("school");
        System.out.println(response.isAcknowledged());
    }

    /**
     * 添加数据
     */
    @Test
    public void testAddData() {
        // 添加数据
        School[] schools = {
                new School("000001", "北京大学", "北京", 120, "孙中山"),
                new School("000002", "清华大学", "北京", 120, "孙伟博"),
                new School("000003", "南京大学", "江苏", 110, "黄炳生"),
                new School("000004", "上海大学", "上海", 80, "黄燕"),
                new School("000005", "江苏大学", "江苏", 80, "刘德兵"),
                new School("000006", "安徽大学", "安徽", 50, "马安丽"),
                new School("000007", "华东理工大学", "上海", 120, "马小龙")
        };
        Arrays.stream(schools).forEach(ElasticsearchUtils::addData);
        // 统一关闭io
        ElasticSearchClient.close();
    }

    @Test
    public void testUpdateData() {
        // 将北京大学校长改成黄家兵
        School school = new School("000001", "北京大学", "北京", 120, "刘德华");
        UpdateResponse response = ElasticsearchUtils.updateData(school);
        System.out.println(response.getResult().name());
    }

    /**
     * 查询
     */
    @Test
    public void testQueryDataByID() {
        GetResponse response = ElasticsearchUtils.getObjectByID("school", "000001");
        System.out.println("id:" + response.getId());
        System.out.println("index:" + response.getIndex());
        System.out.println(response.getSourceAsString());

    }

    /**
     * 删除
     */
    @Test
    public void testDelData() {
        DeleteResponse response = ElasticsearchUtils.deleteObject("shopping", "1002");
        System.out.println(response.getResult().name());
    }

    /**
     * 批量插入
     */
    @Test
    public void testBatchInsertData() {
        List<School> list = new ArrayList<>();
        list.add(new School("000008", "江苏财经大学", "江苏", 120, "马化腾"));
        list.add(new School("000009", "北京理工大学", "北京", 56, "沈小龙"));
        list.add(new School("000010", "南京理工大学", "江苏", 70, "孙德胜"));
        list.add(new School("000011", "江苏理工大学", "江苏", 80, "李云龙"));
        list.add(new School("000012", "安徽理工大学", "安徽", 40, "丁伟"));
        list.add(new School("000013", "浙江理工大学", "浙江", 20, "沈腾"));
        list.add(new School("000014", "苏北理工大学", "黑龙江", 10, "赵刚"));
        list.add(new School("000015", "重庆理工大学", "重庆", 70, "楚云飞"));
        list.add(new School("000016", "东华理工大学", "上海", 80, "孙小腾"));
        list.add(new School("000017", "安阳理工大学", "嘉庆", 10, "孔捷"));
        BulkResponse response = ElasticsearchUtils.batchInsertData(list);
        System.out.println(response.getIngestTookInMillis());
    }

    /**
     * 批量删除
     */
    @Test
    public void testBatchDelObject() {
        List<String> ids = new ArrayList<>();
        ids.add("1003");
        ids.add("1004");
        BulkResponse response = ElasticsearchUtils.batchDelData("shopping", ids);
        System.out.println(response.getIngestTookInMillis());
    }


    @Test
    public void testSearchAll(){
        SearchResponse response = ElasticsearchUtils.searchDataByMatch("school");
        SearchHit[] hits = response.getHits().getHits();
        Arrays.stream(hits).forEach(v -> System.out.println(v.getSourceAsString()));
    }

}
